package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringGroupBuyingDao;
import com.catering.dao.CateringGroupInvitationsDao;
import com.catering.entity.CateringGroupBuying;
import com.catering.entity.CateringGroupInvitations;
import com.catering.entity.vo.CateringGroupBuyingVo;
import com.catering.service.CateringGroupInvitationsService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringGroupInvitationsServiceImpl extends ServiceImpl<CateringGroupInvitationsDao, CateringGroupInvitations> implements CateringGroupInvitationsService {
	
	@Autowired
	private CateringGroupInvitationsDao cateringGroupInvitationsDao;
	@Autowired
	private CateringGroupBuyingDao cateringGroupBuyingDao;

	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper<CateringGroupInvitations> wrapper = CommonUtils.getKeySet(map,"");
			List<CateringGroupInvitations> list = new ArrayList<CateringGroupInvitations>();
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringGroupInvitations> page = new Page<>(Integer.parseInt(map.get("page")),Integer.parseInt(map.get("limit")));
				IPage<CateringGroupInvitations> pageList = cateringGroupInvitationsDao.selectPage(page, wrapper);
				Integer count = cateringGroupInvitationsDao.selectCount(wrapper);
				list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				list = cateringGroupInvitationsDao.selectList(wrapper);
				return ResultData.success(list);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData selectBuying(Map<String,String> map) {
		try {
			QueryWrapper<CateringGroupInvitations> wrapper = CommonUtils.getKeySet(map,"");
			List<CateringGroupInvitations> invitationslist = cateringGroupInvitationsDao.selectList(wrapper);
			List<CateringGroupBuyingVo> newList = new ArrayList<>();
			if(invitationslist != null && invitationslist.size() > 0){
				QueryWrapper queryWrapper = new QueryWrapper<CateringGroupBuying>();
				queryWrapper.in("t1.sys_id",invitationslist.stream().map(CateringGroupInvitations::getGroupBuyingId).collect(Collectors.toList()));
				List<CateringGroupBuyingVo> list = cateringGroupBuyingDao.selectNoPageList(queryWrapper);
				Map<String,CateringGroupBuyingVo> voMap = list.stream().collect(Collectors.toMap(CateringGroupBuyingVo::getSysId, Function.identity()));
				List<CateringGroupInvitations> invitationList = cateringGroupInvitationsDao.selectList(new LambdaQueryWrapper<CateringGroupInvitations>().in(CateringGroupInvitations::getGroupBuyingId,list.stream().map(CateringGroupBuyingVo::getSysId).collect(Collectors.toList())).orderByAsc(CateringGroupInvitations::getGroupBuyingId));
				Map<String,List<CateringGroupInvitations>> maplist = new HashMap<>();
				for(CateringGroupInvitations invi : invitationList){
					if(CommonUtils.checkNoNull(maplist.get(invi.getGroupBuyingId()))){
						List<CateringGroupInvitations> invis = maplist.get(invi.getGroupBuyingId());
						invis.add(invi);
						maplist.put(invi.getSysId(),invis);
					}else{
						List<CateringGroupInvitations> invis = new ArrayList<>();
						invis.add(invi);
						maplist.put(invi.getGroupBuyingId(),invis);
					}
				}
				for(String key : voMap.keySet()){
					CateringGroupBuyingVo newVo = voMap.get(key);
					newVo.setInvitations(maplist.get(key));
					newList.add(newVo);
				}
			}
			return ResultData.success(newList);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringGroupInvitations vo = JSON.parseObject(JSON.toJSONString(map), CateringGroupInvitations.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringGroupInvitationsDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringGroupInvitations vo = JSON.parseObject(JSON.toJSONString(map), CateringGroupInvitations.class);
			cateringGroupInvitationsDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,String> map) {
		try {
			CateringGroupInvitations vo = JSON.parseObject(JSON.toJSONString(map), CateringGroupInvitations.class);
			List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
			cateringGroupInvitationsDao.deleteBatchIds(list);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
